<template>
  <view v-if='goods_info.goods_name' class="goods-detail-container">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for='(image, i) in goods_info.pics' :key='image.pics_id'>
        <image :src='image.pics_big' class="swiper-item" @click="preview(i)"></image>
      </swiper-item>
    </swiper>

    <!-- 商品信息区域 -->
    <view class="goods_info_box">

      <view class="goods_price"> ￥{{ goods_info.goods_price }}</view>

      <view class="goods_title">
        <text class="goods_name">{{ goods_info.goods_name }}</text>
        <view class="favi">
          <uni-icons type='star' size='18' color='gray'></uni-icons>
          <text>收藏</text>
        </view>
      </view>

      <view class="freight">
        <text>快递：免运费{{total}}</text>
      </view>
    </view>

    <!-- 商品详情信息区域 -->
    <!-- 使用 rich-text 组件，将带有 HTML 标签的内容，渲染为小程序的页面结构： -->
    <rich-text :nodes='goods_info.goods_introduce'></rich-text>

    <!-- 底部商品导航区域 -->
    <view class="goods_nav">
      <!-- fill 控制右侧按钮的样式 -->
      <!-- options 左侧按钮的配置项 -->
      <!-- buttonGroup 右侧按钮的配置项 -->
      <!-- click 左侧按钮的点击事件处理函数 -->
      <!-- buttonClick 右侧按钮的点击事件处理函数 -->
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />

    </view>

  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'

  export default {
    data() {
      return {
        goods_info: {},
        options: [{
          icon: 'shop',
          text: '店铺',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]

      }
    },
    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total'])
    },
    onLoad(options) {
      this.getGoodsDetails(options.goods_id)
    },
    // 监听计算属性的变化
    watch: {
      // 使用对象的形式：让页码初次加载时，立马调用total侦听器
      total: {
        handler(newVal) {
          const findResult = this.options.find(x => x.text === '购物车')
          if (findResult) {
            findResult.info = newVal
          }
        },
        // immediate 属性用来声明此侦听器，是否在页面初次加载完毕后立即调用
        immediate: true
      },
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getGoodsDetails(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部 空白间隙的问题
        // 使用字符串的 replace() 方法，将 webp 的后缀名替换为 jpg 的后缀名
        // 正则替换
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
            "<img style='display: block;' ")
          .replace(/webp/g, 'jpg')
        this.goods_info = res.message
      },
      // 点击轮播图放大预览图片
      preview(i) {
        uni.previewImage({
          // 预览时，默认显示图片的索引
          current: i,
          // 所有图片 url地址的数组
          urls: this.goods_info.pics.map(x => x.pics_big)
        })
      },
      // 点击右边按钮触发
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          // 组织一个商品的信息对象
          const {
            goods_info
          } = this
          const goods = {
            goods_name: goods_info.goods_name,
            goods_id: goods_info.goods_id,
            goods_price: goods_info.goods_price,
            goods_small_logo: goods_info.goods_small_logo,
            goods_count: 1, //  商品的数量
            goods_state: true // 商品的勾选状态
          }
          this.addToCart(goods)
        }
      },
      // 点击左边图标触发
      onClick(e) {
        // console.log(e);
        if (e.content.text === '购物车') {
          // 切换到购物车tabBar页面
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      }

    }
  }
</script>

<style lang="scss">
  .goods-detail-container {
    padding-bottom: 50px;

    swiper {
      height: 750rpx;

      .swiper-item {
        width: 100%;
        height: 100%;
      }
    }


    .goods_info_box {
      padding: 10px;
      padding-right: 0;

      .goods_price {
        color: #C00000;
        font-size: 18px;
        margin: 10px 0;
      }

      .goods_title {
        display: flex;
        justify-content: space-between;

        .goods_name {
          font-size: 13px;
          margin-right: 10px;
        }

        .favi {
          width: 120px;
          font-size: 12px;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          border-left: 1px solid #efefef;
          color: gray;
        }
      }

      .freight {
        font-size: 12px;
        color: gray;
        margin: 10px 0;
      }
    }

    .goods_nav {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
    }
  }
</style>
